package com.cssw.fyzb.busi.api.live;

import com.cssw.fyzb.core.exception.BizException;
import com.cssw.fyzb.core.model.vo.live.AudienceVo;
import com.cssw.fyzb.busi.service.live.LiveService;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Feng Chen
 */
@Function(description = "查询观众(观众端)", parameters = {
        @Parameter(name = "liveId", type = Type.Integer, description = "房间标识", required = true),
        @Parameter(name = "num", type = Type.Integer, description = "返回数量", required = false),
        @Parameter(name = "userId", type = Type.Integer, description = "用户标识", required = true),
})
@Component
public class ListAudience implements Action {

    @Autowired
    private LiveService liveService;

    private static final Logger logger = LoggerFactory.getLogger(ListAudience.class);

    @Override
    public Response execute(Request request) throws Exception {
        Long liveId = request.getLong("liveId");
        Integer num = request.getInteger("num");
        Long userId = request.getLong("userId");
        try {
            List<AudienceVo> audienceVos = liveService.listAudience(liveId, userId, num);
            return Response.success().put("list", audienceVos).put("total", 1).put("count", audienceVos.size());
        } catch (Exception e) {
            if (e instanceof BizException) {
                BizException be = (BizException) e;
                return Response.fail(be.getErrMsg(), be.getErrCode());
            }
            logger.error("", e);
            return Response.fail("操作异常");
        }
    }

}
